package com.swlz.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.swlz.commons.DBUtil;
import com.swlz.entity.MallType;

import java.util.Date;
/**
 * <pre>
 * 数据访问对象
 * 商城产品类别
 * TableName : MALL_TYPE
 * ClassName : com.swlz.dao.MallTypeDAO
 * Author : swlz
 * E-Mail : 2233875735@qq.com
 * Date : 2020-06-27 22:33:28
 * Version : 1.0
 * Copyright 2020 swlz.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes
 * </pre>
 **/
public class MallTypeDAO {

	/**
	 * 添加一个商城产品类别
	 * 包含所有字段
	 * @param mallType - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insert(MallType mallType) throws SQLException {
		if(mallType.getMallTypeId()==null) {
			mallType.setMallTypeId(DBUtil.nextId());
		}
		return DBUtil.insert(mallType);
	}
 
	
	/**
	 * 添加一个商城产品类别
	 * 只包含非null的字段
	 * @param mallType - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insertSelective(MallType mallType) throws SQLException {
		if(mallType.getMallTypeId()==null) {
			mallType.setMallTypeId(DBUtil.nextId());
		}
		return DBUtil.insertSelective(mallType);
	}
	

	/**
	 * 根据主键更新一个商城产品类别
	 * 包含所有字段
	 * @param mallType - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int update(MallType mallType) throws SQLException {
		return DBUtil.update(mallType);
	}
	
	/**
	 * 根据主键更新一个商城产品类别
	 * 只包含非null的字段
	 * @param mallType - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int updateSelective(MallType mallType) throws SQLException {
		return DBUtil.updateSelective(mallType);
	}
	
	/**
	 * 根据主键删除一个商城产品类别
	 * @param mallTypeId - 主键
	 * @return
	 * @throws SQLException
	 */
	public int deleteById(Long mallTypeId) throws SQLException  {
		return DBUtil.execute("DELETE FROM MALL_TYPE WHERE MALL_TYPE_ID=?", mallTypeId);
	}
	
	/**
	 * 根据主键获取一个商城产品类别
	 * @param mallTypeId - 主键
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public MallType selectById(Long mallTypeId) throws SQLException  {
		return DBUtil.get("SELECT * FROM MALL_TYPE WHERE MALL_TYPE_ID=?", MallType.class ,mallTypeId);
	}

		
	/**
	 * 根据模型中的非空字段查询
	 * @param mallTypeId - 实体对象
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public MallType selectByModel(MallType mallType)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM MALL_TYPE WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(mallType,sql,params);
		return DBUtil.get(sql.toString(), MallType.class, params.toArray());
	}
	
	/**
	 * 根据模型中的非空字段查询
	 * @param mallTypeId - 实体对象
	 * @param index - 开始索引
	 * @param len - 返回长度
	 * @return
	 * @throws SQLException
	 */
	public static List<MallType> selectByModel(MallType mallType,int index,int len)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM MALL_TYPE WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(mallType,sql,params);
		sql.append(" ORDER BY MALL_TYPE_ID ASC LIMIT ?,?");
		params.add(index);
		params.add(len);
		return DBUtil.list(sql.toString(), MallType.class, params.toArray());
	}

	/**
	 * 根据模型中的非空字段统计
	 * @param mallTypeId - 实体对象
	 * @return
	 * @throws SQLException
	 */
	public long countByModel(MallType mallType)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT count(*) as c FROM MALL_TYPE WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(mallType,sql,params);
		return DBUtil.getLong(sql.toString(), params.toArray());
	}
	
	/**
	 * 格式化where条件和参数
	 * @param mallTypeId - 实体对象
	 * @param sql - SQL语句
	 * @param params - SQL语句中的参数
	 */
	private static void where(MallType mallType,StringBuffer sql,List<Object> params) {
		if(mallType==null){
			return;
		}

		if(mallType.getMallTypeId()!=null) {
			sql.append(" AND MALL_TYPE_ID=?");
			params.add(mallType.getMallTypeId());
		}

		if(mallType.getMallTypeCode()!=null) {
			sql.append(" AND MALL_TYPE_CODE=?");
			params.add(mallType.getMallTypeCode());
		}

		if(mallType.getMallTypeName()!=null) {
			sql.append(" AND MALL_TYPE_NAME=?");
			params.add(mallType.getMallTypeName());
		}

		if(mallType.getMallTypeParentId()!=null) {
			sql.append(" AND MALL_TYPE_PARENT_ID=?");
			params.add(mallType.getMallTypeParentId());
		}

	}
}